import axios from "axios";
import loading from "./loading";
import { getProjectManager } from "@/scripts/getProjectManager";

const getModelserIp = () => {
    const objm = getProjectManager()
    return objm.modelserManager.modelserUrl.match(/^(https?:\/\/[^\/]+)/)?.[1]
}

// 获取Modelser服务列表
export function listServer(params: any = {}, sort: any = {}, filter: any = {}) {

    const getInfoData = axios({
        url: `${getModelserIp()}/api/server/list`,
        method: "post",
        headers: {
            "Content-Type": "application/json",
        },
        data: { params: params, sort: sort, filter: filter },
    })
    return getInfoData;

}

// 根据id获取服务
export function getServer(id: any) {

    const getInfoData = axios({
        url: `${getModelserIp()}/api/server/get?id=${id}`,
        method: "post",
        headers: {
            "Content-Type": "application/json",
        },
    })
    return getInfoData;

}

//提交编辑任务
export function EditorSubmit(data: any) {
    loading.showLoading("正在提交任务，请稍后...")

    const getInfoData = axios({
        url: `${getModelserIp()}/api/editor/submit`,
        method: 'post',
        data,
        headers: {
            "Content-Type": "application/json",
        }
    })
    return getInfoData;
}

//获取某个项目正在运行的任务进度
export function getTasksRunning(id: any) {
    const getInfoData = axios({
        url: `${getModelserIp()}/api/task/running?projectid=${id}`,
        method: 'post',

    })
    return getInfoData;
}

//查询编辑任务
export function reqGetRecordlist(data: any) {
    const getInfoData = axios({
        url: `${getModelserIp()}/api/editor/list`,
        method: 'post',
        data,
        headers: {
            "Content-Type": "application/json",
        }
    })
    return getInfoData
}

// 删除编辑任务
export function reqDeleteRecordList(data: any) {
    loading.showLoading("正在删除任务，请稍后...")

    const getInfoData = axios({
        url: `${getModelserIp()}/api/editor/remove`,
        method: 'post',
        data,
        headers: {
            "Content-Type": "application/json",
        }
    })
    return getInfoData
}

//   取消正在进行的任务
export function reqCancelTask(id: any) {
    const result = axios({
        url: `${getModelserIp()}/api/task/cancel?taskid=${id}`,
        method: 'post',
    })
    return result
}